package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.FlinkNodeResponse;
import com.sh.data.engine.application.model.request.datadev.ChangeDirRequest;
import com.sh.data.engine.application.model.request.datadev.FlinkNodeBaseRequest;
import com.sh.data.engine.application.model.request.datadev.SortNodeRequest;
import com.sh.data.engine.application.service.datadev.FlinkNodeApplicationService;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.datadev.flink.model.domain.FlinkDirTreeDomain;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: zigui.zdf
 * @description:
 * @date: 2021/6/2 17:35
 */
@Validated
@RestController
@RequestMapping("/flink/node")
@Tag(name = "数据开发-实时计算节点", description = "实时计算节点相关API")
public class FlinkNodeController {

    @Autowired
    private FlinkNodeApplicationService flinkNodeApplicationService;

    @PostMapping("/saveNode")
    @Operation(summary = "保存目录、FINK SQL 、FLINK三种类型节点")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> saveDir(@Valid FlinkNodeBaseRequest request) {
        var result = flinkNodeApplicationService.saveDir(request);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/changeNodeDir")
    @Operation(summary = "移动节点至其他目录")
    @LogExecution(operation = EngineOperation.REMOVE, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.REMOVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> changeNodeDir(@Valid ChangeDirRequest request) {
        var result = flinkNodeApplicationService.changeNodeDir(request);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/changeNodeSort")
    @Operation(summary = "节点排序")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> changeNodeSort(@Valid SortNodeRequest request) {
        var result = flinkNodeApplicationService.changeNodeSort(request);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getAllNodes")
    @Operation(summary = "获取所有节点")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<List<FlinkNodeResponse>> getAllNodes(
            @Parameter(description = "类型,0-任务 1-资源 2-函数", required = true) @RequestParam Integer type,
            @Parameter(description = "0-仅资源列表标志 ", required = false) @RequestParam(required = false)
            Integer resourceFlag) {
        var result = flinkNodeApplicationService.getAllNodes(type, resourceFlag);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDirNodes")
    @Operation(summary = "获取目录树列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<List<FlinkDirTreeDomain>> getDirNodes(
            @Parameter(description = "类型,0-任务 1-资源 2-函数", required = true) @RequestParam Integer type) {
        var result = flinkNodeApplicationService.getDirNodes(type);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/deleteNode")
    @Operation(summary = "删除节点")
    @LogExecution(operation = EngineOperation.DEL, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.DEL)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.DEVELOP_TASK_ONLINE)
    public RestResponseEntity<Boolean> deleteNode(
            @Parameter(description = "节点id", required = true) @RequestParam Long id) {
        var result = flinkNodeApplicationService.deleteNode(id);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/lockNode")
    @Operation(summary = "锁定/解锁")
    @LogExecution(operation = EngineOperation.LOCK_OR_UNLOCK, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.LOCK_OR_UNLOCK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> lockNode(
            @Parameter(description = "节点id", required = true) @RequestParam Long id,
            @Parameter(description = "是否锁住，0-解锁 1-锁住", required = true) @RequestParam Integer isLocked) {
        var result = flinkNodeApplicationService.lockNode(id, isLocked);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/publish")
    @Operation(summary = "发布")
    @LogExecution(operation = EngineOperation.PUBLISH, business = Business.实时任务)
//    @Log(
//        business = Business.实时任务,
//        operation = Operation.PUBLISH)
//    @AuthorityAnnotation(menuId = MenuEnumNew.SSKF, readWriteStatus = 1, isAdd = 1)
    public RestResponseEntity<Boolean> publish(
            @Parameter(description = "节点id", required = true) @RequestParam Long id,
            @Parameter(description = "0-未发布 1-已发布", required = true) @RequestParam Integer isPublished) {
        var result = flinkNodeApplicationService.publish(id, isPublished);
        return RestResponseEntity.success(result);
    }
}
